package com.runtime.hongyunuser.controller;

import com.runtime.hongyunuser.bean.*;
import com.runtime.hongyunuser.service.ProductService;
import com.runtime.hongyunuser.service.ShoppingcartsService;
import com.runtime.hongyunuser.service.UserFavoriteService;
import com.runtime.hongyunuser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private UserFavoriteService userFavoriteService;

    @PostMapping("/add")
    public Result addToFavorite(@RequestBody Map<String,Long> favorites) {
        try {
            Long userId=favorites.get("userId");
            Long productId=favorites.get("productId");
            Userfavorites UFItem = userFavoriteService.addFavorites(userId, productId);
            return new Result(Code.SAVE_OK, UFItem, "添加成功");
        } catch (Exception e) {
            return new Result(Code.SAVE_ERR, null, e.getMessage());
        }
    }

    @GetMapping("/check")
    public Result checkStatus(@RequestBody Map<String,Long> favorites) {
        Long userId = favorites.get("userId");
        Long productId = favorites.get("productId");
        boolean isCollected = userFavoriteService.isCollected(userId, productId);
        String msg = isCollected ? "查询成功" : "查询失败";
        int code = isCollected ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, isCollected, msg);
    }

    // 切换收藏状态
    @PostMapping("/toggle")
    public Result toggleFavorite(@RequestBody Map<String,Long> favorites) {
        Long userId = favorites.get("userId");
        Long productId = favorites.get("productId");
        boolean isCollected = userFavoriteService.toggleCollect(userId, productId);
        String msg = isCollected ? "操作成功" : "操作失败";
        int code = isCollected ? Code.SAVE_OK : Code.SAVE_ERR;
        return new Result(code, isCollected, msg);
    }

    @GetMapping("SelectByproductID")
    public Result selectProductByKeyword(@RequestParam("keyword") String keyword) {


        List<Products> result = productService.selectProduct(keyword);
        boolean flag = result.size() > 0;
        String msg = flag ? "查询成功" : "查询失败";
        int code = flag ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, result, msg);
    }

    @GetMapping("selectProductFavorite")
    public Result selectProductFavorite(HttpServletRequest req) {
        long userId;
        Object loginAcc = req.getSession().getAttribute("loginAcc");
        if(loginAcc != null) {
            // 假设result是User类型
            Users user = (Users)loginAcc;
            // 现在可以使用user对象了
             userId = user.getUserId();

            List<Userfavorites> result = productService.selectProductFavorite(userId);
            boolean flag = result.size() > 0;
            String msg = flag ? "查询成功" : "查询失败";
            int code = flag ? Code.GET_OK : Code.GET_ERR;
            return new Result(code, result, msg);
        }
        return null;
    }
    //添加收藏夹
    @PostMapping("addFavorite")
    public Result addProductFavorite(@RequestParam("productID") Integer productID,HttpServletRequest req) {
        long userId;
        Object loginAcc = req.getSession().getAttribute("loginAcc");
        if(loginAcc != null) {
            // 假设result是User类型
            Users user = (Users)loginAcc;
            // 现在可以使用user对象了
            userId = user.getUserId();
            int result = productService.addProductFavorite(productID,userId);
            boolean flag = result > 0;
            String msg = flag ? "添加成功" : "添加失败";
            int code = flag ? Code.GET_OK : Code.GET_ERR;
            return new Result(code, result, msg);
        }
        return null;
    }
//移出收藏夹
    @DeleteMapping("removeFavorite/{productID}")
public Result deleteProductFavorite(@PathVariable("productID") Integer productID,HttpServletRequest req) {
        long userId;
        Users user = (Users) req.getSession().getAttribute("loginAcc");
        if(user != null) {

            // 现在可以使用user对象了
            userId = user.getUserId();
            int result = productService.deleteProductFavorite(productID,userId);
            boolean flag = result > 0;
            String msg = flag ? "删除成功" : "删除失败";
            int code = flag ? Code.GET_OK : Code.GET_ERR;
            return new Result(code, result, msg);
        }
        return null;
    }



    /**
     * 获取最新产品
     */
    @GetMapping("/new")
    public Result getNewProducts() {
        List<Products> products = productService.getNewProducts();
        boolean flag = products.size() > 0;
        int code = flag ? Code.GET_OK : Code.GET_ERR;
        String msg = flag ? "获取成功" : "获取失败";

        return new Result(code, products, msg);
    }

    /**
     * 获取推荐产品
     */
    @GetMapping("/featured")
    public Result getFeaturedProducts() {
        List<Products> products = productService.getFeaturedProducts();
        boolean flag = products.size() > 0;
        int code = flag ? Code.GET_OK : Code.GET_ERR;
        String msg = flag ? "获取成功" : "获取失败";
        return new Result(code, products, msg);
    }

    /**
     * 获取价值产品
     */
    @GetMapping("/value")
    public Result getValueProducts() {
        List<Products> products = productService.getValueProducts();
        boolean flag = products.size() > 0;
        int code = flag ? Code.GET_OK : Code.GET_ERR;
        String msg = flag ? "获取成功" : "获取失败";

        return new Result(code, products, msg);
    }

    /**
     * 获取设备产品
     */
    @GetMapping("/device")
    public Result getDeviceProducts() {
        List<Products> products = productService.getDeviceProducts();
        boolean flag = products.size() > 0;
        int code = flag ? Code.GET_OK : Code.GET_ERR;
        String msg = flag ? "获取成功" : "获取失败";

        return new Result(code, products, msg);
    }

    /**
     * 获取促销产品
     */
    @GetMapping("/promotion")
    public Result getPromotionProducts() {
        List<Products> products = productService.getPromotionProducts();
        boolean flag = products.size() > 0;
        int code = flag ? Code.GET_OK : Code.GET_ERR;
        String msg = flag ? "获取成功" : "获取失败";

        return new Result(code, products, msg);
    }

    /**
     * 获取产品图片
     */
    @GetMapping("/image")
    public Result getProductImage(Integer productId) {
        Productimages products = productService.getProductImages(productId);
        boolean flag = products != null;
        int code = flag ? Code.GET_OK : Code.GET_ERR;
        String msg = flag ? "获取成功" : "获取失败";
        return new Result(code, products, msg);
    }
}
